package server

import (
	"chitchat_http/model"
	"errors"
	"fmt"
	"github.com/doujunyu/gogo/gogo_log"
	"github.com/doujunyu/gogo/sql_aid"
	"strings"
)

// SystemByMusicByAuthorList 歌曲作者列表
func SystemByMusicByAuthorList(userId int64 ,page string, pageSize string) []map[string]interface{}{
	sql,args := sql_aid.PgTable(model.TableByMusicAuthor).PageSize(page,pageSize).ToSql()
	authorData, err := sql_aid.DataToMap(model.PgSql.Query(sql,args...))
	if err != nil {
		gogo_log.Error("server_SystemToMusicByAuthorList", "查询数据失败", err.Error())
		return []map[string]interface{}{}
	}
	return authorData
}
// SystemByMusicByAuthorAdd 歌曲作者添加
func SystemByMusicByAuthorAdd(userId int64 ,author string ) error{
	author = strings.Trim(author," ")
	sql,args := sql_aid.PgTable(model.TableByMusicAuthor).Where("name = ?",author).ToSql()
	authorAata, err := sql_aid.DataToMap(model.PgSql.Query(sql,args...))
	if err != nil || authorAata != nil {
		return errors.New("该作者已经存在，无需再次进行添加")
	}
	//进行添加
	dataInsert := make(map[string]interface{})
	dataInsert["name"] = author
	sql,args = sql_aid.PgTable(model.TableByMusicAuthor).InsertByMap(dataInsert)
	tx, err := model.PgSql.Begin()
	if err != nil {
		gogo_log.Error("server_SystemToMusicByAuthorAdd", "打开数据库失败", err.Error())
		return errors.New("添加信息失败")
	}
	_, err = tx.Exec(sql, args...)
	if err != nil {
		gogo_log.Error("server_SystemToMusicByAuthorAdd", "新增数据失败", err.Error())
		return errors.New("执行失败")
	}
	tx.Commit()
	return nil
}
// SystemByMusicByAuthorEdit 歌曲作者编辑
func SystemByMusicByAuthorEdit(id int64 ,userId int64 ,author string)error{
	author = strings.Trim(author," ")
	sql,args := sql_aid.PgTable(model.TableByMusicAuthor).Where("name = ?",author).ToSql()
	authorAata, err := sql_aid.DataToMap(model.PgSql.Query(sql,args...))
	if err != nil || authorAata != nil {
		return errors.New("该作者已经存在，无需再次进行添加")
	}

	dataUpdate := make(map[string]interface{})
	dataUpdate["name"] = author
	sql,args = sql_aid.PgTable(model.TableByMusicAuthor).Where("id = ?",id).UpdateByMap(dataUpdate)
	tx, err := model.PgSql.Begin()
	if err != nil {
		gogo_log.Error("server_SystemToMusicByAuthorEdit", "打开数据库失败", err.Error())
		return errors.New("添加信息失败")
	}
	_, err = tx.Exec(sql, args...)
	if err != nil {
		gogo_log.Error("server_SystemToMusicByAuthorEdit", "新增数据失败", err.Error())
		return errors.New("执行失败")
	}
	tx.Commit()
	return nil
}
// SystemByMusicByAuthorDel 歌曲作者删除
func SystemByMusicByAuthorDel(id int64 ,userId int64)error{
	//检测作者是否还有歌曲存在
	sql,args := sql_aid.PgTable(model.TableByMusic).Where("music_author_id @> ?",fmt.Sprintf("[{\"author_id\":%v}]", id)).ToSql()
	teamData, err := sql_aid.DataToMap(model.PgSql.Query(sql,args...))
	if err != nil || len(teamData) > 0{
		return errors.New("删除失败，当前作者还有音乐没有移除！")
	}
	//删除操作
	sql,args = sql_aid.PgTable(model.TableByMusicAuthor).Where("id = ?",id).Delete()
	tx, err := model.PgSql.Begin()
	if err != nil {
		gogo_log.Error("server_SystemToMusicByAuthorEdit", "打开数据库失败", err.Error())
		return errors.New("添加信息失败")
	}
	_, err = tx.Exec(sql, args...)
	if err != nil {
		gogo_log.Error("server_SystemToMusicByAuthorEdit", "新增数据失败", err.Error())
		return errors.New("执行失败")
	}
	tx.Commit()
	return nil
}

// SystemByMusicByTeamList 乐队列表
func SystemByMusicByTeamList(userId int64 ,page string, pageSize string)[]map[string]interface{}{
	sql,args := sql_aid.PgTable(model.TableByMusicTeam).PageSize(page,pageSize).ToSql()
	teamData, err := sql_aid.DataToMap(model.PgSql.Query(sql,args...))
	if err != nil {
		gogo_log.Error("server_SystemToMusicByTeamList", "查询数据失败", err.Error())
		return []map[string]interface{}{}
	}
	return teamData
}
// SystemByMusicByTeamAdd 乐队添加
func SystemByMusicByTeamAdd(userId int64 ,team string) error{
	team = strings.Trim(team," ")
	sql,args := sql_aid.PgTable(model.TableByMusicTeam).Where("name = ?",team).ToSql()
	teamData, err := sql_aid.DataToMap(model.PgSql.Query(sql,args...))
	if err != nil || teamData != nil {
		return errors.New("乐队已经存在，无需再次进行添加")
	}
	//进行添加
	dataInsert := make(map[string]interface{})
	dataInsert["name"] = team
	sql,args = sql_aid.PgTable(model.TableByMusicTeam).InsertByMap(dataInsert)
	tx, err := model.PgSql.Begin()
	if err != nil {
		gogo_log.Error("server_SystemToMusicByAuthorAdd", "打开数据库失败", err.Error())
		return errors.New("添加信息失败")
	}
	_, err = tx.Exec(sql, args...)
	if err != nil {
		gogo_log.Error("server_SystemToMusicByAuthorAdd", "新增数据失败", err.Error())
		return errors.New("执行失败")
	}
	tx.Commit()
	return nil
}
// SystemByMusicByTeamEdit 乐队编辑
func SystemByMusicByTeamEdit(id int64 ,userId int64 ,team string)error{
	team = strings.Trim(team," ")
	sql,args := sql_aid.PgTable(model.TableByMusicTeam).Where("name = ?",team).ToSql()
	teamData, err := sql_aid.DataToMap(model.PgSql.Query(sql,args...))
	if err != nil || teamData != nil {
		return errors.New("乐队已经存在，无需再次进行添加")
	}
	dataInsert := make(map[string]interface{})
	dataInsert["name"] = team
	sql,args = sql_aid.PgTable(model.TableByMusicTeam).Where("id = ?",id).UpdateByMap(dataInsert)
	tx, err := model.PgSql.Begin()
	if err != nil {
		gogo_log.Error("server_SystemToMusicByAuthorAdd", "打开数据库失败", err.Error())
		return errors.New("添加信息失败")
	}
	_, err = tx.Exec(sql, args...)
	if err != nil {
		gogo_log.Error("server_SystemToMusicByAuthorAdd", "新增数据失败", err.Error())
		return errors.New("执行失败")
	}
	tx.Commit()
	return nil
}
// SystemByMusicByTeamDel 乐队删除
func SystemByMusicByTeamDel(id int64 ,userId int64)error{
	//检测作者是否还有歌曲存在
	sql,args := sql_aid.PgTable(model.TableByMusic).Where("music_team_id = ?", id).ToSql()
	teamData, err := sql_aid.DataToMap(model.PgSql.Query(sql,args...))
	if err != nil || len(teamData) > 0{
		return errors.New("删除失败，当前作者还有音乐没有移除！")
	}
	//删除操作
	sql,args = sql_aid.PgTable(model.TableByMusicTeam).Where("id = ?",id).Delete()
	tx, err := model.PgSql.Begin()
	if err != nil {
		gogo_log.Error("server_SystemToMusicByTeamDel", "打开数据库失败", err.Error())
		return errors.New("添加信息失败")
	}
	_, err = tx.Exec(sql, args...)
	if err != nil {
		gogo_log.Error("server_SystemToMusicByTeamDel", "删除数据失败", err.Error())
		return errors.New("执行失败")
	}
	tx.Commit()
	return nil
}




